﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ServiceModel;
using PumeCentralService;
using Domain = PumpCentral_Domain;
using Threading = System.Windows.Threading;
using System.Diagnostics;
using PumpCentral_Domain;
using System.Collections.ObjectModel;


namespace PumpCentral_Gui
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window ,IDisposable
    {
        CollectionViewSource viewsoruce;
        PumpCentralSystem system = new PumpCentralSystem();
        IPumpCentalService ServiceConnection;

        public MainWindow()
        {
            InitializeComponent();
            Threading.DispatcherTimer alarmtimer = new Threading.DispatcherTimer();
            alarmtimer.Tick += alarmtimer_Tick;
            alarmtimer.Start();
            system.PumpRegistered += system_PumpRegistered;

            viewsoruce = (CollectionViewSource)(this.Resources["listingDataView"]);
        }

        void system_PumpRegistered(Pump pump)
        {
            ((App)Application.Current).PumpStations.Add(pump);
        }

        void alarmtimer_Tick(object sender, EventArgs e)
        {
            
        }

        private void OnPumpsSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int index = ListBox_Pumps.SelectedIndex;

            //var pumps = Application.Current.Resources.FindName("PumpStations");      

        }

        private void SubscribeToService()
        {
            ChannelFactory<IPumpCentalService> ServiceChannelFactory = new ChannelFactory<IPumpCentalService>();

            try
            {
                ServiceConnection = ServiceChannelFactory.CreateChannel();
                bool success = ServiceConnection.Subscribe();
                
                if (!success)
                {
                    MessageBox.Show("Connection Failed");
                    
                }

                
            }
            catch(CommunicationException ce)
            {
                MessageBox.Show("Login Failed");
            }
        }

        public void Dispose()
        {
            ChannelFactory<IPumpCentalService> ServiceChannelFactory = new ChannelFactory<IPumpCentalService>();
            try
            {
                IPumpCentalService centralservice = ServiceChannelFactory.CreateChannel();
                centralservice.UnSubscribeCentral();
            }
            catch(CommunicationException ce)
            {
                MessageBox.Show("unsubscription failed if the service is still running you are fucked!!!");
            }
        }

        private void Btn_GetSingle_Click(object sender, RoutedEventArgs e)
        {
            
        }

        private void btn_Getall_Click(object sender, RoutedEventArgs e)
        {
            List<Pump> pumps = system.GetDataFromAllPumps();
            
            ((App)Application.Current).PumpStations.Clear();

            foreach (Pump pump in pumps)
            {
                ((App)Application.Current).PumpStations.Add(pump);
            }
        }

        
    }
}
